﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class lensStockRelation : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            PanelInput.Visible = false;
            binddataBrandName();
            binddataStockDepotName();
            binddataStockBrandName();
            binddataStockLensName();
        }
    }

    private void binddataBrandName()
    {
        DropDownListBrand.DataSource = CSUtils.getAllBrandList(false);
        DropDownListBrand.DataBind();
    }

    private void binddataStockDepotName()
    {
        DropDownListStockDepot.DataSource = CSUtils.getAllDepotList("品牌");
        DropDownListStockDepot.DataBind();
    }

    private void binddataStockBrandName()
    {
        DropDownListStockBrand.DataSource = CSUtils.getAllBrandList(true);
        DropDownListStockBrand.DataBind();
    }

    private void binddataStockLensName()
    {
        DropDownListStockLens.DataSource = CSUtils.getAllLensList("全部");
        DropDownListStockLens.DataBind();
    }



    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        PanelInput.Visible = false;
        PanelRef.Visible = false;
        this.binddata();
    }

    private void binddata() {
        string brandID = DropDownListBrand.SelectedValue;
        GridView1.DataSource = this.getAllLensStockRelation(brandID);
        GridView1.DataBind();
        PanelAction.Visible = true;

    }


    private DataSet getAllLensStockRelation(string brandID)
    {
        string selectedBrandID = DropDownListBrand.SelectedValue;
        string sql =
            "select re.id, lb.lens_id, " +
            "   lb.lens_name, br2.brand_name, stock_depot_id, stock_brand_id, stock_lens_id, " +
            "   depot_name as stock_depot_name, br.brand_name as stock_brand_name, lb2.lens_name as stock_lens_name " +
            "from cs_lens_base lb " +
            "left join cs_lens_stock_relation re on re.brand_id = " + brandID + " and lb.lens_id = re.lens_id " +
            "left join cs_depots d on stock_depot_id = d.depot_id " +
            "left join cs_brands br on stock_brand_id = br.brand_id " +
            "left join cs_lens_base lb2 on stock_lens_id = lb2.lens_id " +
            "left join cs_brands br2 on br2.brand_id = " + selectedBrandID +
            " {0} " +
            "order by lb.lens_name";

        string where = "where lb.visible = 1 ";

        string lensName = TextBoxLensName_Q.Text;
        if (lensName.Trim() != "")
        {
            where += "and lb.lens_name like '%" + lensName + "%' ";
        }

        string refractive = DropDownListrRefractive_Q.SelectedValue;
        if (refractive.Trim() != "-1")
        {
            where += "and lb.refractive_index = '" + refractive + "' ";
        }

        sql = string.Format(sql, where);


        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {



        if (e.CommandName.Equals("command_del"))
        {


        }
        else if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add"))
        {

            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;

            

            GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);   //获取被点击的linkButton所在的GridViewRow
            int index = gvrow.RowIndex;                                                         //获取到行索引 RowIndex

            //获取当前行的信息
            string ID = e.CommandArgument.ToString();
            if (ID == "") { ID = "0"; }
            string lensID = GridView1.Rows[index].Cells[0].Text;
            string brandName = GridView1.Rows[index].Cells[1].Text;
            string lensName = GridView1.Rows[index].Cells[2].Text;


            LabelID.Text = ID;
            LabelLensID.Text = lensID;
            LabelBrandName.Text = brandName;
            LabelLensName.Text = lensName;

            LabelLensID.Text = lensID;
            //string[] tmp = e.CommandArgument.ToString().Split(',');
            //string ID = tmp[0];
            //string lensID = tmp[1];
            //LabelID.Text = ID;
            //LabelLensID.Text = lensID;
            //LabelLensName.Text = CSUtils.getLens(lensID).Name;
            setInput(ID);
        }
        else
        {
            this.cleanPanelInput();
        }
    }



    protected void ButtonSubmit_Click(object sender, EventArgs e) {
        updateLensStockRelation();
        PanelInput.Visible = false;
        this.binddata();
    }

    private bool updateLensStockRelation()
    {
        string ID = LabelID.Text;
        string brandID = DropDownListBrand.SelectedValue;
        string lensID = LabelLensID.Text;
        string stockDepotID = DropDownListStockDepot.SelectedValue;
        string stockBrandID = DropDownListStockBrand.SelectedValue;
        string stockLensID = DropDownListStockLens.SelectedValue;

        string sql_insert =
            "insert into cs_lens_stock_relation(brand_id, lens_id, stock_depot_id, stock_brand_id, stock_lens_id) " +
            "values({0},{1}, {2}, {3}, {4})";

        string sql_update =
            "update cs_lens_stock_relation set stock_depot_id = {1}, stock_brand_id = {2}, stock_lens_id = {3} " +
            "where id = {0}";

        string sql = "";

        // Insert
        if (ID == "0")
        {
            sql = string.Format(sql_insert, brandID, lensID, stockDepotID, stockBrandID, stockLensID);
        }
        // Update
        else
        {
            sql = string.Format(sql_update, ID, stockDepotID, stockBrandID, stockLensID);
        }

        DBConn myDB = new DBConn();
        int tmp;
        try
        {
            tmp = myDB.Update(sql);
        }
        catch
        {
            myDB.Close();
            return false;
        }
        myDB.Close();
        return (tmp > 0);
    }


    private void cleanPanelInput() {
        LabelID.Text = "";
        LabelLensID.Text = "";
        LabelLensName.Text = "";
    }

    private void setInput(string ID)
    {
        if (ID == "0") {
            return;
        }

        string sql =
            "select * from cs_lens_stock_relation where id = " + ID;

        DBConn myDB = new DBConn();
        SqlDataReader dr = myDB.getDataReader(sql);
        if (dr.Read())
        {
            DropDownListStockDepot.SelectedValue = dr["stock_depot_id"].ToString();
            DropDownListStockBrand.SelectedValue = dr["stock_brand_id"].ToString();
            DropDownListStockLens.SelectedValue = dr["stock_lens_id"].ToString(); 
        }
        dr.Close();
        myDB.Close();

    }

    protected void LinkButtonRef_Click(object sender, EventArgs e)
    {
        //PanelInput.Visible = false;
        //PanelRef.Visible = true;
        //DropDownListRef_Brand.DataSource = this.getRefBrand();
        //DropDownListRef_Brand.DataBind();
    }

    //private DataSet getRefBrand()
    //{
    //    string lensID = DropDownListLens.SelectedValue;
    //    string sql =
    //        "select distinct b.brand_id, c.brand_name from cs_lens_price_base b " +
    //        "left join cs_brands c on b.brand_id = c.brand_id " +
    //        "order by b.brand_id";

    //    DataSet ds = new DataSet();
    //    DBConn myDB = new DBConn();
    //    ds = myDB.getDataSet(sql);
    //    myDB.Close();
    //    return ds;
    //}


    protected void ButtonRef_Click(object sender, EventArgs e)
    {

        //string brandID = DropDownListBrand.SelectedValue;
        //string lensID = DropDownListLens.SelectedValue;
        //string refBrandID = DropDownListRef_Brand.SelectedValue;
        //string refLensID = DropDownListRef_Lens.SelectedValue;

        //if (brandID == refBrandID && lensID == refLensID)
        //{
        //    LabelErrorMsg.Text = "不能自己参照自己！";
        //    return;
        //}

        //if (this.doRef(brandID, lensID, refBrandID, refLensID))
        //{
        //    LabelErrorMsg.Text = "";
        //}
        //else
        //{
        //    LabelErrorMsg.Text = "参照失败！";
        //    return;
        //}

        //this.binddata();

    }

    //private bool doRef(string brandID, string lensID, string refBrandID, string refLensID)
    //{

    //    string sql =
    //        "delete from cs_lens_price_base where brand_id = {0} and lens_id = {1};" +
    //        "INSERT INTO cs_lens_price_base " +
    //        "   ([brand_id], [lens_id], [sphere_begin], [sphere_end], [cylinder_begin], [cylinder_end], [diameter], [sale_price], [agent_price], [cost1], [cost2], [cost3], [cost4], [cost5]) " +
    //        "   select {0}as brand_id, {1}as lens_id, [sphere_begin], [sphere_end], [cylinder_begin], [cylinder_end], [diameter], [sale_price], [agent_price], [cost1], [cost2], [cost3], [cost4], [cost5] " +
    //        "   from cs_lens_price_base " +
    //        "   where brand_id = {2} and lens_id = {3}";

    //    sql = string.Format(sql, brandID, lensID, refBrandID, refLensID);

    //    DBConn myDB = new DBConn();
    //    int tmp = myDB.Update(sql);
    //    myDB.Close();
    //    return (tmp > 0);
    //}

}
